#[=======================================================================[.rst:
FindCQUPT
---------

Find CQUPT (Compact Quick Unified Protocol for Transfer) library.

IMPORTED Targets
^^^^^^^^^^^^^^^^

This module defines the following :prop_tgt:`IMPORTED` targets:

``CQUPT::CQUPT``
  The CQUPT library, if found.

Result Variables
^^^^^^^^^^^^^^^^

This module will set the following variables in your project:

``CQUPT_FOUND``
  True if CQUPT library is found.
``CQUPT_INCLUDE_DIRS``
  Include directories for CQUPT headers.
``CQUPT_LIBRARIES``
  Libraries to link for CQUPT.
``CQUPT_VERSION``
  Version of CQUPT library found.

Hints
^^^^^

``CQUPT_DIR``
  Set this variable to the root installation of CQUPT. When set, will
  automatically configure CQUPT_INCLUDE_DIR and CQUPT_LIBRARY_DIR.

``CQUPT_ROOT``
  Alternative to CQUPT_DIR for backward compatibility.

#]=======================================================================]

# Handle CQUPT_DIR variable - automatically set related paths
if(CQUPT_DIR)
    set(CQUPT_ROOT ${CQUPT_DIR})
    # Auto-configure common paths when CQUPT_DIR is set
    if(NOT CQUPT_INCLUDE_DIR)
        set(CQUPT_INCLUDE_DIR ${CQUPT_DIR}/include)
    endif()
    if(NOT CQUPT_LIBRARY_DIR)
        set(CQUPT_LIBRARY_DIR ${CQUPT_DIR}/lib)
    endif()
    message(STATUS "CQUPT_DIR set to: ${CQUPT_DIR}")
    message(STATUS "Auto-configured CQUPT include dir: ${CQUPT_INCLUDE_DIR}")
    message(STATUS "Auto-configured CQUPT library dir: ${CQUPT_LIBRARY_DIR}")
endif()

# Find include directory
find_path(CQUPT_INCLUDE_DIR
    NAMES cqupt.h
    PATHS
        ${CQUPT_ROOT}
        ${CQUPT_ROOT}/include
        $ENV{CQUPT_ROOT}
        $ENV{CQUPT_ROOT}/include
        ${CQUPT_DIR}
        ${CQUPT_DIR}/include
        $ENV{CQUPT_DIR}
        $ENV{CQUPT_DIR}/include
        /usr/include
        /usr/local/include
        ${CMAKE_INSTALL_PREFIX}/include
    PATH_SUFFIXES
        CQUPT
        cqupt
    DOC "CQUPT include directory"
)

# Find library
find_library(CQUPT_LIBRARY
    NAMES CQUPT cqupt
    PATHS
        ${CQUPT_ROOT}
        ${CQUPT_ROOT}/lib
        $ENV{CQUPT_ROOT}
        $ENV{CQUPT_ROOT}/lib
        ${CQUPT_DIR}
        ${CQUPT_DIR}/lib
        ${CQUPT_LIBRARY_DIR}
        $ENV{CQUPT_DIR}
        $ENV{CQUPT_DIR}/lib
        /usr/lib
        /usr/local/lib
        ${CMAKE_INSTALL_PREFIX}/lib
    PATH_SUFFIXES
        lib
        lib64
    DOC "CQUPT library"
)

# Try to extract version from header file
if(CQUPT_INCLUDE_DIR)
    if(EXISTS "${CQUPT_INCLUDE_DIR}/cqupt.h")
        file(STRINGS "${CQUPT_INCLUDE_DIR}/cqupt.h" CQUPT_VERSION_LINE
            REGEX "#define[ \t]+CQUPT_VERSION[ \t]+[0-9]+"
        )
        if(CQUPT_VERSION_LINE)
            string(REGEX REPLACE "#define[ \t]+CQUPT_VERSION[ \t]+([0-9]+).*" "\\1"
                CQUPT_VERSION "${CQUPT_VERSION_LINE}"
            )
        endif()
    endif()
endif()

# Handle standard arguments
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(CQUPT
    FOUND_VAR CQUPT_FOUND
    REQUIRED_VARS CQUPT_LIBRARY CQUPT_INCLUDE_DIR
    VERSION_VAR CQUPT_VERSION
)

# Set output variables
if(CQUPT_FOUND)
    set(CQUPT_INCLUDE_DIRS ${CQUPT_INCLUDE_DIR})
    set(CQUPT_LIBRARIES ${CQUPT_LIBRARY})
    
    # Create imported target
    if(NOT TARGET CQUPT::CQUPT)
        add_library(CQUPT::CQUPT STATIC IMPORTED)
        set_target_properties(CQUPT::CQUPT PROPERTIES
            IMPORTED_LOCATION "${CQUPT_LIBRARY}"
            INTERFACE_INCLUDE_DIRECTORIES "${CQUPT_INCLUDE_DIR}"
        )
        
        # Add platform-specific libraries
        if(WIN32)
            set_target_properties(CQUPT::CQUPT PROPERTIES
                INTERFACE_LINK_LIBRARIES "wsock32;ws2_32"
            )
        endif()
    endif()
endif()

# Mark variables as advanced
mark_as_advanced(
    CQUPT_INCLUDE_DIR
    CQUPT_LIBRARY
)

message(STATUS "CQUPT library found: ${CQUPT_FOUND}")
message(STATUS "CQUPT include directories: ${CQUPT_INCLUDE_DIRS}")
message(STATUS "CQUPT libraries: ${CQUPT_LIBRARIES}")
message(STATUS "CQUPT version: ${CQUPT_VERSION}")

